package com.py.payment.controller;

import com.py.payment.dto.param.PyPayRecordsParamDto;
import com.py.payment.entity.PyPayRecords;
import com.py.payment.service.PyPayRecordsService;
import com.py.payment.tools.date.DateUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/payMent")
public class PayMentController {

    @Autowired
    private PyPayRecordsService payRecordsService;

    @PostMapping(name = "新增支付台账",path = "/addPayMent")
    public ResponseData addPayMent(@RequestBody PyPayRecords payRecords){
        this.payRecordsService.addPayMent(payRecords);
        return ResponseData.success();
    }

    @PostMapping(name = "保存支付台账",path = "/savePayMent")
    public ResponseData savePayMent(@RequestBody PyPayRecordsParamDto payRecordsParamDto){
        PyPayRecords payRecords = new PyPayRecords();
        if(payRecordsParamDto.getPayCycle()==1){
            String time = payRecordsParamDto.getTime();
            payRecords.setPayTime(DateUtils.datestrToDate(time,"yyyy"));
            payRecords.setTitle(time+"年支付");
        } else {
            String time = payRecordsParamDto.getTime();
            Date payTime = DateUtils.datestrToDate(time, "yyyy-MM");

            Calendar c = Calendar.getInstance();
            c.setTime(payTime);

            payRecords.setPayTime(payTime);
            StringBuffer sb = new StringBuffer();
            sb.append(c.get(Calendar.YEAR)).append("年").append(c.get(Calendar.MONTH)+1).append("期支付");
            payRecords.setTitle(sb.toString());
        }
        payRecords.setInfoId(payRecordsParamDto.getInfoId());
        payRecords.setAccountsPayable(payRecordsParamDto.getAccountsPayable());
        payRecords.setNetPayment(payRecordsParamDto.getNetPayment());
        payRecords.setRemarks(payRecordsParamDto.getRemarks());
        payRecords.setType(payRecordsParamDto.getType());
        this.payRecordsService.addPayMent(payRecords);
        return ResponseData.success();
    }

}
